﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using Castle.Core;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using MvcContrib.Castle;

namespace ProductInfoMVC
{
	// Note: For instructions on enabling IIS6 or IIS7 classic mode, 
	// visit http://go.microsoft.com/?LinkId=9394801
	public class MvcApplication : System.Web.HttpApplication, IContainerAccessor
	{
		private static WindsorContainer c_container; 
		

		public static IWindsorContainer Container { get { return c_container; } } 		
		

		IWindsorContainer IContainerAccessor.Container { get { return Container; } } 


		protected void Application_Start()
		{
			InitializeContainer();
			
			RegisterRoutes(RouteTable.Routes);
		}
		

		private static void InitializeContainer()
		{
			//TODO Add components through code
			// See http://blog.coreycoogan.com/2009/11/06/castle-windsor-tutorial-in-asp-net-mvc/
			if (c_container == null)
			{
				c_container = new WindsorContainer(new XmlInterpreter());
				ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(Container));
				c_container.RegisterControllers(typeof(ProductInfo.Core.Controllers.ReleaseNoteEntryController).Assembly);
				
			}
		}


		public static void RegisterRoutes(RouteCollection routes)
		{
			routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

			routes.MapRoute(
				"Default",                                              // Route name
				"{controller}/{action}/{id}",                           // URL with parameters
				new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
			);
		}
	}
}